package dao

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"goplus/errors"
)

type User struct {
	id       int
	username string
	password string
}

var db *sql.DB

func init() {
	var err error
	db, err = sql.Open("mysql", "root:123456@tcp(127.0.0.1:3306)/tp")
	if err != nil {
		panic(err)

	}
}

func FindUser(id int) (User, error) {
	sqlStr := "select id, username, password from user where id=?"
	var u User
	err := db.QueryRow(sqlStr, id).Scan(&u.id, &u.username, &u.password)
	if err == sql.ErrNoRows {
		return u, errors.New(errors.NOTFOUND, fmt.Sprintf("no user id is %d", id))
	}
	if err != nil {
		return u, errors.New(errors.INTERNAL_ERROE, fmt.Sprintf("no user id is %d", id))
	}
	return u, nil
}
